﻿using SYSoft.Model;
using SYSoft.Model.Models.DeviceManage;
using SYSoft.Model.Models.SystemManage;
using SYSoft.Repository;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace SYSoft.Services.DeviceManage
{


    public interface IDeviceAlarmServices : IBaseServices<DeviceAlarmInfo>
    {
        /// <summary>
        /// 分页多条件报警查询
        /// </summary>
        /// <param name="deviceid"></param>
        /// <param name="starttime"></param>
        /// <param name="stoptime"></param>
        /// <param name="alarmdegree"></param>
        /// <param name="page"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        Task<PageModel<DeviceAlarmInfo>> conditionsSelect(int deviceid, DateTime starttime, DateTime stoptime, int alarmdegree, int page, int size);

        /// <summary>
        /// 多条件报警查询
        /// </summary>
        /// <param name="deviceid"></param>
        /// <param name="starttime"></param>
        /// <param name="stoptime"></param>
        /// <param name="alarmdegree"></param>
        /// <returns></returns>
        Task<List<DeviceAlarmInfo>> listConditionsSelect(int deviceid, DateTime starttime, DateTime stoptime, int alarmdegree);

        /// <summary>
        /// 设备报警类别信息分布统计 
        /// </summary>
        /// <param name="deviceid"></param>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <param name="alarmDegree"></param>
        /// <returns>返回前端图表所需的字典</returns>
        List<Dictionary<string, object>> alarmDistribution(List<DeviceAlarmInfo> alarmInfos);
    }





    public class DeviceAlarmServices : BaseServices<DeviceAlarmInfo>, IDeviceAlarmServices
    {
        public DeviceAlarmServices(IBaseRepository<DeviceAlarmInfo> BaseDal) : base(BaseDal)
        {

        }

        public async Task<PageModel<DeviceAlarmInfo>> conditionsSelect(int deviceid, DateTime starttime, DateTime stoptime, int alarmdegree, int page, int size)
        {
            if(alarmdegree==0)
            {
                return await QueryPage((d => d.DeviceId == deviceid && d.AlarmStatus==0 && d.CreatTime > starttime && d.CreatTime < stoptime), page, size);
            }
            else
            {
                return await QueryPage((d => d.DeviceId == deviceid && d.AlarmStatus == 0 && d.CreatTime > starttime && d.CreatTime < stoptime && d.AlarmDegree==alarmdegree), page, size);
            }
          
        }

        public async Task<List<DeviceAlarmInfo>> listConditionsSelect(int deviceid, DateTime starttime, DateTime stoptime, int alarmdegree)
        {
            if (alarmdegree == 0)
            {
                return await Query(d => d.DeviceId == deviceid && d.AlarmStatus == 0 && d.CreatTime > starttime && d.CreatTime < stoptime);
            }
            else
            {
                return await Query(d => d.DeviceId == deviceid && d.AlarmStatus == 0 && d.CreatTime > starttime && d.CreatTime < stoptime && d.AlarmDegree == alarmdegree);
            }
              
        }



      
        public List<Dictionary<string, object>> alarmDistribution(List<DeviceAlarmInfo> alarmInfos)//(int deviceid, DateTime startTime, DateTime endTime, int alarmDegree)
        {
            string[] category = new string[] { "设备掉线", "停机", "设备故障", "急停" };
            List<Dictionary<string, object>> Maps = new List<Dictionary<string, object>>();

            int baseNumber = 0; // 基础报警数量
            for (int i = 0; i < category.Length; i++)
            {
                Dictionary<string, object> map = new Dictionary<string, object>();
                int count = alarmInfos.FindAll(x => x.AlarmCode == category[i]).Count;
                //饼状图的key-value
                map.Add("name", category[i]);  
                map.Add("number", count);
                Maps.Add(map);
                baseNumber += count;
            }

            Dictionary<string, object> map2 = new Dictionary<string, object>();
            // 除基本报警类型之外的归为“其他”类型
            map2.Add("name", "其他");
            map2.Add("number", alarmInfos.Count- baseNumber);
            Maps.Add(map2);

            return Maps;
        }
    }
}
